-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang][analysis] Thread Safety Analysis: Handle parenthesis #140656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@llvm/pr-subscribers-clang-static-analyzer-1 Author: Prabhu Rajasekaran (Prabhuk) ChangesWhen the variable guared by a lock was enclosed in parenthesis, access Full diff: https://github.com/llvm/llvm-project/pull/140656.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index 7e86af6b4a317..156df51a71012 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -1671,7 +1671,7 @@ void ThreadSafetyAnalyzer::checkAccess(const FactSet &FSet, const Expr *Exp,
// Guard against self-initialization. e.g., int &i = i;
if (E == Exp)
break;
- Exp = E;
+ Exp = E->IgnoreImplicit()->IgnoreParenCasts();
continue;
}
}
diff --git a/clang/test/Analysis/thread-safety-handle-parenthesis.cpp b/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
new file mode 100644
index 0000000000000..6be8362a650e0
--- /dev/null
+++ b/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
@@ -0,0 +1,20 @@
+
+// RUN: %clang_cc1 -verify -fsyntax-only -std=c++20 -Wthread-safety %s
+
+class __attribute__((lockable)) Lock {};
+
+void sink_protected(int) {}
+
+class Baz {
+public:
+ Lock lock_;
+ int protected_num_ __attribute__((guarded_by(lock_))) = 1;
+};
+
+void baz_paran_test() {
+ Baz baz;
+ int& n = baz.protected_num_;
+ sink_protected(n); // expected-warning{{reading variable 'protected_num_' requires holding mutex 'baz.lock_'}}
+ int& n2 = (baz.protected_num_);
+ sink_protected(n2); // expected-warning{{reading variable 'protected_num_' requires holding mutex 'baz.lock_'}}
+}
|
|
@llvm/pr-subscribers-clang-analysis Author: Prabhu Rajasekaran (Prabhuk) ChangesWhen the variable guared by a lock was enclosed in parenthesis, access Full diff: https://github.com/llvm/llvm-project/pull/140656.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index 7e86af6b4a317..156df51a71012 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -1671,7 +1671,7 @@ void ThreadSafetyAnalyzer::checkAccess(const FactSet &FSet, const Expr *Exp,
// Guard against self-initialization. e.g., int &i = i;
if (E == Exp)
break;
- Exp = E;
+ Exp = E->IgnoreImplicit()->IgnoreParenCasts();
continue;
}
}
diff --git a/clang/test/Analysis/thread-safety-handle-parenthesis.cpp b/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
new file mode 100644
index 0000000000000..6be8362a650e0
--- /dev/null
+++ b/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
@@ -0,0 +1,20 @@
+
+// RUN: %clang_cc1 -verify -fsyntax-only -std=c++20 -Wthread-safety %s
+
+class __attribute__((lockable)) Lock {};
+
+void sink_protected(int) {}
+
+class Baz {
+public:
+ Lock lock_;
+ int protected_num_ __attribute__((guarded_by(lock_))) = 1;
+};
+
+void baz_paran_test() {
+ Baz baz;
+ int& n = baz.protected_num_;
+ sink_protected(n); // expected-warning{{reading variable 'protected_num_' requires holding mutex 'baz.lock_'}}
+ int& n2 = (baz.protected_num_);
+ sink_protected(n2); // expected-warning{{reading variable 'protected_num_' requires holding mutex 'baz.lock_'}}
+}
|
|
@llvm/pr-subscribers-clang Author: Prabhu Rajasekaran (Prabhuk) ChangesWhen the variable guared by a lock was enclosed in parenthesis, access Full diff: https://github.com/llvm/llvm-project/pull/140656.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index 7e86af6b4a317..156df51a71012 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -1671,7 +1671,7 @@ void ThreadSafetyAnalyzer::checkAccess(const FactSet &FSet, const Expr *Exp,
// Guard against self-initialization. e.g., int &i = i;
if (E == Exp)
break;
- Exp = E;
+ Exp = E->IgnoreImplicit()->IgnoreParenCasts();
continue;
}
}
diff --git a/clang/test/Analysis/thread-safety-handle-parenthesis.cpp b/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
new file mode 100644
index 0000000000000..6be8362a650e0
--- /dev/null
+++ b/clang/test/Analysis/thread-safety-handle-parenthesis.cpp
@@ -0,0 +1,20 @@
+
+// RUN: %clang_cc1 -verify -fsyntax-only -std=c++20 -Wthread-safety %s
+
+class __attribute__((lockable)) Lock {};
+
+void sink_protected(int) {}
+
+class Baz {
+public:
+ Lock lock_;
+ int protected_num_ __attribute__((guarded_by(lock_))) = 1;
+};
+
+void baz_paran_test() {
+ Baz baz;
+ int& n = baz.protected_num_;
+ sink_protected(n); // expected-warning{{reading variable 'protected_num_' requires holding mutex 'baz.lock_'}}
+ int& n2 = (baz.protected_num_);
+ sink_protected(n2); // expected-warning{{reading variable 'protected_num_' requires holding mutex 'baz.lock_'}}
+}
|
When the variable guared by a lock was enclosed in parenthesis, access violation warnings were not emitted. This patch fixes it and adds a regression test.
74dbb15 to
8570d7a
Compare
steakhal
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks.
When the variable guared by a lock was enclosed in parenthesis, access
violation warnings were not emitted. This patch fixes it and adds a
regression test.